package ifrn.pdsd.android.extranet;

import ifrn.pdsd.android.model.Funcionarios;
import java.util.ArrayList;
import java.util.List;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListarFuncActivity extends ListActivity {
	
	public static final int MENU1 = Menu.FIRST;
	
	private static final String NAMESPACE = "http://tempuri.org/";
	private static final String METHOD_NAME = "ListarUsuarios";
	private static final String SOAP_ACTION = "http://tempuri.org/ListarUsuarios";
	private static final String URL = LoginActivity.enderecoWS;
	private static List<Funcionarios> listaFuncionarios;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_listar_funcionarios);

		this.realizarBusca();
	}
	
	private void realizarBusca() {
		listaFuncionarios = new ArrayList<Funcionarios>();
		
		try {
            SoapObject requisicao = new SoapObject(NAMESPACE, METHOD_NAME);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(requisicao);

            HttpTransportSE http = new HttpTransportSE(URL); 
            http.call(SOAP_ACTION, envelope);
            
            SoapObject response = (SoapObject) envelope.getResponse();
            Log.w("tamanho atual array", String.valueOf(response.getPropertyCount()));
            	Log.w("array eh diferente", "array eh diferente");
            	for(int i = 0; i < response.getPropertyCount(); i++){
                    SoapObject objSoap = (SoapObject)response.getProperty(i);
                    
                    Funcionarios f = new Funcionarios();
                    f.setId(Integer.parseInt(objSoap.getProperty(0).toString()));
                    f.setNome(objSoap.getProperty(1).toString());
                    listaFuncionarios.add(f);
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
		
        setListAdapter(new ArrayAdapter<Funcionarios>(this, android.R.layout.simple_list_item_1, listaFuncionarios));
	}
	
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	
    	Object o = this.getListAdapter().getItem(position);
    	Funcionarios f = (Funcionarios) o;
    	
    	Intent intent = new Intent(this, ListarTarefasByFuncActivity.class);
    	intent.putExtra("idFuncionario", f.getId());
    	startActivity(intent);
    }
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU1, 0, "Voltar");
		return super.onCreateOptionsMenu(menu);
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU1:
			startActivity(new Intent(ListarFuncActivity.this,
					MenuActivity.class));
			return true;
		}
		return false;
	}

}
